package cn.dinkevin.xui.net.exception;

import cn.dinkevin.xui.ErrorInfo;
import cn.dinkevin.xui.util.StringUtil;

/**
 * Create with Android Studio
 * Author: chengpengfei
 * Date: 2018/5/6
 * Time: 11:12 AM
 * Description:
 */
public class BizException extends RuntimeException{

    private int code;
    private String msg;

    private static int BIZ_OK = 200;

    /**
     * 绑定业务 OK 代码
     * @param code
     */
    public static void setBizOk(int code){
        BIZ_OK = code;
    }

    public BizException(int code, String msg) {
        super("code: " + code + ", message: " + msg);
        this.code = code;
        this.msg = msg;
        if(StringUtil.isEmpty(msg)){
            this.msg = ErrorInfo.getMessage(code);
        }
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return msg;
    }

    public boolean isBizError() {
        return this.code != BIZ_OK;
    }

    @Override
    public String toString() {
        return "BizException{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                '}';
    }
}
